//
//  MylibraryViewController.m
//  录音
//
//  Created by ywy on 2016/12/22.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "MylibraryViewController.h"
#import "REServerViewController.h"// 服务器VC
#import "RELocalViewController.h"// 本地文件VC
#import "ReTopTitleView.h" // titleView
#import "REOperationView.h" // 背景的图片
#import "RECheckLogin.h"

@interface MylibraryViewController ()<UIScrollViewDelegate>

@property(nonatomic,assign) NSInteger number;
@property(nonatomic,strong) NSArray * titleArr;
@property(nonatomic,strong) REOperationView * reOperationView ;

@end

@implementation MylibraryViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"i"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
    self.navigationItem.rightBarButtonItem = right;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"标题阴影"]];
   
}
-(void)rightClick
{
  
    self.reOperationView.hidden = NO;
    if (self.number == 0) {
      // 第一个
        NSURL * url = [NSURL URLWithString:@"http://116.255.239.201:8080/musicing/aboutus/disPlayDetail?id=2"];
        [self.reOperationView.webView loadRequest:[NSURLRequest requestWithURL:url]];
    }else
    {
        // 第二个
        NSURL * url = [NSURL URLWithString:@"http://116.255.239.201:8080/musicing/aboutus/disPlayDetail?id=2"];
        [self.reOperationView.webView loadRequest:[NSURLRequest requestWithURL:url]];
    }
    
}

- (void)viewDidLoad {
     [super viewDidLoad];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popVC)   name:@"popTwo" object:nil];
     _titleArr = @[@"服务器文件",@"本地文件"];
     self.number = 0;
     [self setUpScrollView];
     self.reOperationView= [[REOperationView alloc] initWithFrame:self.view.bounds];
     self.reOperationView.hidden = YES;
     [self.reOperationView .deleteButton addTarget:self action:@selector(btnClik) forControlEvents:UIControlEventTouchUpInside];
     [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.reOperationView];
}
-(void)popVC
{
     CGPoint offset = CGPointMake(REScreenW, 0);
    [_contentScrollerView setContentOffset:offset animated:YES];
    [self performSelector:@selector(applictionWithNotifitionPushController) withObject:self afterDelay:0.5];
//   [[NSNotificationCenter defaultCenter] removeObserver:self name:@"popVC" object:nil];
}
-(void)applictionWithNotifitionPushController
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"popThree" object:nil ];
}

-(void)btnClik
{
    self.reOperationView.hidden = YES;
}

-(void)setUpScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 添加底部的 滑动 视图的
    _topScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, REScreenW, 40)];
    _topScrollerView.showsHorizontalScrollIndicator=NO;
    _topScrollerView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_topScrollerView];
    //2. 添加大的滚动菜单栏
    _contentScrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 110, REScreenW, REScreenH-64)];
    _contentScrollerView.showsHorizontalScrollIndicator=NO;
    _contentScrollerView.delegate=self;
    _contentScrollerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_contentScrollerView];
    //3.添加子控制器
    [self addController];
    //4.添加标题栏
    [self addTitleView];
    //5.设置大的scrollview的滚动范围
    CGFloat contentX = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    _contentScrollerView.contentSize = CGSizeMake(contentX, 0);
    _contentScrollerView.pagingEnabled = YES;
    //6.添加默认子控制器 (也就是第一个)
    REServerViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = _contentScrollerView.bounds;
    ReTopTitleView * view = _topScrollerView.subviews[0];
    view.lineLabel.hidden = NO;
    [_contentScrollerView addSubview:vc.view];
    
}
-(void)addController
{
    for (NSInteger i = 0; i<_titleArr.count; i++) {
        if (i==0) {
            REServerViewController * ServerVC = [[REServerViewController alloc] init];
            [self addChildViewController:ServerVC];
        }else{
            RELocalViewController * LocalVC = [[RELocalViewController alloc] init];
            [self addChildViewController:LocalVC];
        }
    }
}
-(void)addTitleView
{
    CGFloat lblX =0;
    CGFloat titleViewWith = REScreenW/_titleArr.count;
    
    for (NSInteger i = 0 ; i< _titleArr.count; i++) {
        lblX = i * titleViewWith;
        ReTopTitleView * titleView = [[ReTopTitleView alloc] initWithFrame:CGRectMake(lblX, 0, titleViewWith, 32)];
        titleView.titleLabel.text = _titleArr[i];
        [_topScrollerView addSubview:titleView];
        titleView.tag = i;
        titleView.userInteractionEnabled = YES;
        [titleView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleViewClick:)]];
        UIView * backView = [[UIView alloc] init];
        backView.backgroundColor = RERGBColor(241, 241, 241);
        backView.alpha = 0.5;
        if (i == 0) {
            backView.frame = CGRectMake(10, 32, titleViewWith-10, 2);
            [titleView addSubview:backView];
        }else
        {
            backView.frame = CGRectMake(0, 32, titleViewWith-10, 2);
            [titleView addSubview:backView];
        }
    }
}

-(void)titleViewClick:(UITapGestureRecognizer*)tap
{
    
    ReTopTitleView *titleView = (ReTopTitleView *)tap.view;
    for ( ReTopTitleView *titleViewTwo in _topScrollerView.subviews) {
        titleViewTwo.lineLabel.hidden = YES;
    }
    titleView.lineLabel.hidden = NO;
    CGFloat offsetX = titleView.tag * _contentScrollerView.frame.size.width;
    CGFloat offsetY = _contentScrollerView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    
    [_contentScrollerView setContentOffset:offset animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayFirst" object:nil];
}
-(void)titleViewClickNumber:(NSInteger)number
{
    
    
    [self setUpScrollView];
//    ReTopTitleView *titleView = (ReTopTitleView *)[self.view viewWithTag:1];
//    for ( ReTopTitleView *titleViewTwo in _topScrollerView.subviews) {
//        titleViewTwo.lineLabel.hidden = YES;
//    }
//    titleView.lineLabel.hidden = NO;
    CGFloat offsetX = 1 * _contentScrollerView.frame.size.width;
    
    CGFloat offsetY = _contentScrollerView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [_contentScrollerView setContentOffset:offset animated:YES];
    
}

#pragma mark - ******************** scrollView代理方法
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / _contentScrollerView.frame.size.width;
    for ( ReTopTitleView *titleView in _topScrollerView.subviews) {
        titleView.lineLabel.hidden = YES;
    }
     self.number = index;
    // 滚动标题栏
    ReTopTitleView *titleLable = (ReTopTitleView *)_topScrollerView.subviews[index];
    titleLable.lineLabel.hidden = NO;

    //    [_topScrollerView setContentOffset:CGPointMake(index*YPScreenW/_titleArr.count, 0) animated:YES];
    // 添加控制器
    REServerViewController *newsVc = self.childViewControllers[index];
    if (newsVc.view.superview) return;
    newsVc.view.frame = scrollView.bounds;
    [_contentScrollerView addSubview:newsVc.view];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayFirst" object:nil];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / _contentScrollerView.frame.size.width;
    for ( ReTopTitleView *titleView in _topScrollerView.subviews) {
        titleView.lineLabel.hidden = YES;
    }
          self.number = index;
    // 滚动标题栏
    ReTopTitleView *titleLable = (ReTopTitleView *)_topScrollerView.subviews[index];
    titleLable.lineLabel.hidden = NO;
    //    [_topScrollerView setContentOffset:CGPointMake(index*YPScreenW/_titleArr.count, 0) animated:YES];
    // 添加控制器
    REServerViewController *newsVc = self.childViewControllers[index];
    if (newsVc.view.superview) return;
    newsVc.view.frame = scrollView.bounds;
    
    [_contentScrollerView addSubview:newsVc.view];
 [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayFirst" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
